package com.mayikt.main.api.impl;

import com.mayikt.common.core.api.BaseApiService;
import com.mayikt.common.core.api.BaseResponse;
import com.mayikt.common.core.utils.MeiteBeanUtils;
import com.mayikt.common.core.utils.RedisUtil;
import com.mayikt.main.api.UserInfoService;
import com.mayikt.main.api.dto.res.UserResDto;
import com.mayikt.main.api.impl.entity.SysUser;
import com.mayikt.main.api.impl.mapper.SysUserMapper;
import org.apache.logging.log4j.util.Strings;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RestController;


@RestController
public class UserInfoServiceImpl extends BaseApiService implements UserInfoService {
    @Autowired
    private SysUserMapper sysUserMapper;

    @Override
    public BaseResponse<UserResDto> getByTokenUserInfo(String token) {
        if (Strings.isBlank(token)) {
            return setResultError("token is null");
        }
        //根据用户的token获取redis的userid
        String redisUserId = RedisUtil.getString(token);
        if (Strings.isBlank(redisUserId)) {
            return setResultError("token is null");
        }
        SysUser user = sysUserMapper.selectById(Integer.parseInt(redisUserId));
        UserResDto resDto = MeiteBeanUtils.doToDto(user, UserResDto.class);
        return setResultSuccessData(resDto);
    }
}
